package com.zzptc.syl.my;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zzptc.syl.my.entity.RecipeMode1;
import com.zzptc.syl.my.entity.ResultsBean;
import com.zzptc.syl.my.service.MyAppService;

import org.litepal.LitePal;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author 宋雅丽
 */
public class MainActivity extends AppCompatActivity {
    List<ResultsBean> allResultsBeanlist;
    private static final String TAG = "MainActivity";
    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("https://liyuankun.cn/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=findViewById(R.id.lv);
        getRecipeInfo();
        getRecipeInfoFromDB();
        MyAdapter myAdapter=new MyAdapter();
        listView.setAdapter(myAdapter);

    }

    private void getRecipeInfoFromDB() {
         allResultsBeanlist = LitePal.findAll(ResultsBean.class);
    }

    private void getRecipeInfo() {
        MyAppService myAppService = retrofit.create(MyAppService.class);
        Call<RecipeMode1> allRecope =myAppService.getAllRecipe(5);
        allRecope.enqueue(new Callback<RecipeMode1>() {
            @Override
            public void onResponse(Call<RecipeMode1> call, Response<RecipeMode1> response) {
                RecipeMode1 recipeMode1= response.body();
                assert recipeMode1 != null;
                Log.d(TAG,"onResponse："+recipeMode1.getResults().size());
                List<ResultsBean> results = recipeMode1.getResults();
                for(ResultsBean r:results){
                    r.save();
                }
            }

            @Override
            public void onFailure(Call<RecipeMode1> call, Throwable t) {

            }
        });
    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return allResultsBeanlist.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            @SuppressLint("ViewHolder")
            View view=View.inflate(MainActivity.this,R.layout.recipmode1_item,null);
            ImageView ivPic =view.findViewById(R.id.lv_pic);
            TextView tvTitel =view.findViewById(R.id.tv_title);
            ResultsBean resultsBean=allResultsBeanlist.get(position);
            String cover=resultsBean.getCover();//图片地址
            Glide.with(MainActivity.this)
                    .load(cover)
                    .into(ivPic );
            tvTitel .setText(resultsBean.getTitle());
            return view;
        }
    }

}
